package com.aistar.controller;

import com.aistar.mapper.TravCollectMapper;
import com.aistar.mapper.TravelnoteMapper;
import com.aistar.pojo.TravCollect;
import com.aistar.pojo.Travelnote;
import com.aistar.service.redis_collect.RedisCollectService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;

/**
 * @author shkstart
 * @date 2019/10/16 - 1:35
 */
@Controller
public class C_RedisController {

    @Autowired
    RedisCollectService redisCollectService;

    @Autowired
    TravCollectMapper travCollectMapper;

    @Autowired
    TravelnoteMapper travelnoteMapper;
//    从游记列表点击链接跳进游记详情
    @RequestMapping("traveldetails/traveldetails/{userId}/{noteId}")
    public ModelAndView travelDetails(@PathVariable("userId") int userId, @PathVariable("noteId") int noteId){
        int status = 0;
        TravCollect travCollect = travCollectMapper.selectByPrimaryKey(noteId,userId);
        if(travCollect==null || (travCollect!=null && travCollect.getCollectStatus()==0)){
            status = 0;
        }
        if(travCollect!=null && travCollect.getCollectStatus()==1){
            status = 1;
        }
        Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(noteId);
        ModelAndView mov = new ModelAndView();
        mov.addObject("status",status);
        mov.addObject("userId",userId);
        mov.addObject("noteId",noteId);
        if(travelnote.getTravOther2() == null){
            mov.addObject("num",0);
        }
        else {
            mov.addObject("num",travelnote.getTravOther2());
        }
        System.out.println("travelnote.getTravOther2():"+travelnote.getTravOther2());
        mov.setViewName("traveldetails/traveldetails");

//        mov.setViewName("test");
        return mov;
    }

//    返回json数据：游记详情，点赞
    @PostMapping("traveldetails/collect")
    @ResponseBody
    public int collectServicetest_add(int userId,int noteId){
        try{
            redisCollectService.saveLiked2Redis(String.valueOf(userId),String.valueOf(noteId));
            System.out.println("返回json数据ok");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
//    返回json数据：游记详情，取消点赞
    @PostMapping("traveldetails/discollect")
    @ResponseBody
    public int collectServicetest_auter(int userId,int noteId){
        try{
            redisCollectService.unlikeFromRedis(String.valueOf(userId),String.valueOf(noteId));
            System.out.println("discollect：返回json数据ok");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }


}
